﻿using System;
using System.Windows.Forms;

namespace monitor_server_2
{
    public partial class AddUser : Form
    {
        AccessList.OkAddUserDelegate del;
        bool newUser;

        public AddUser(AccessList.OkAddUserDelegate d, bool newser, string data)
        {            
            InitializeComponent();
            accessList.DropDownStyle = ComboBoxStyle.DropDownList;
            accessList.Items.Add("Администратор");
            accessList.Items.Add("Пользователь");
            button2.Text = Main.Rm.GetString("Cancel");
            label1.Text = Main.Rm.GetString("UserName");
            label2.Text = Main.Rm.GetString("Password");
            label3.Text = Main.Rm.GetString("Access");
            label4.Text = Main.Rm.GetString("AdditionalInfo");
            newUser = newser;
            if (newUser)
            {
                this.Text = Main.Rm.GetString("AddUser");
                button1.Text = Main.Rm.GetString("Add");
                button3.Visible = false;
            }
            else
            {
                this.Text = Main.Rm.GetString("ChangeUserData");
                button1.Text = Main.Rm.GetString("Accept");
                button3.Text = Main.Rm.GetString("Delete");
                button3.Visible = true;
                string[] split = data.Split(';');
                loginBox.Text = split[0];
                accessList.SelectedIndex = int.Parse(split[1]) - 1;
                //accessList.SelectedItem = int.Parse(split[1]);
                infoBox.Text = split[2];
            }
            newUser = newser;
            del = d;            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (loginBox.Text.Length < 1)
            {
                MessageBox.Show(Main.Rm.GetString("EnterUserName"));
                return;
            }
            if (passwordBox.Text.Length < 1)
            {
                MessageBox.Show(Main.Rm.GetString("EnterPassword"));
                return;
            }
            if (accessList.SelectedIndex < 0)
            {
                MessageBox.Show(Main.Rm.GetString("ChooseAccess"));
                return;
            }
            if (!newUser)
                del(loginBox.Text, "", "", "", false);
            del(loginBox.Text, passwordBox.Text, (accessList.SelectedIndex + 1).ToString(), infoBox.Text, true); 
            DialogResult = DialogResult.OK;            
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (loginBox.Text.Length < 1)
            {
                MessageBox.Show(Main.Rm.GetString("EnterUserName"));
                return;
            }
            del(loginBox.Text, "", "", "", false);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}
